package com.cup.bddm.impala.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.handler.Token;
import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.service.NationService;
import com.cup.bddm.impala.vo.NationBranchTransVo;
import com.cup.bddm.impala.vo.NationGeneDailyTransVo;
import com.cup.bddm.util.DateUtil;

@Token
@Controller("srv-NationController")
@RequestMapping(value = "/services/nation", method = RequestMethod.GET)
public class NationController {
	static Logger logger = Logger.getLogger(NationController.class);

	@Autowired
	private NationService nationService;

	@RequestMapping("/general")
	@ResponseBody
	public NationGeneDailyTransVo getNationGeneDailyTransInfo() {
		logger.info("nation top branch trans request");
		NationGeneDailyTransVo result = nationService.getNationGeneralTransInfo();
		return result;
	}

	@RequestMapping("/topBranch/{date}/{type}")
	@ResponseBody
	public NationBranchTransVo getTopBranchTransInfo(@PathVariable("date") String date, @PathVariable("type") String type) {
		logger.info("nation top branch trans request. date=" + date + ", type=" + type);
		NationBranchTransVo result = new NationBranchTransVo();
		if (StringUtils.isEmpty(date) || StringUtils.isEmpty(type)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		result = nationService.getTopBranchTransInfo(date, type);
		return result;
	}

}
